import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'dart:convert';
import '../config/service_url.dart';
// 所有方法全部写到此文件 方法返回数据

Future getBlogPage() async {
  try {
    print("开始获取博客数据");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["blogpage"]);
    if (response.statusCode == 200) {
      print(response);
      return json.decode(response.toString())["data"];
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getBlogDocs(String id) async {
  try {
    print("开始获取博客数据");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["blogdocs"] + id);
    if (response.statusCode == 200) {
      print(response);
      return json.decode(response.toString())["data"];
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getBookPage() async {
  try {
    print("开始获取内容页面");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["bookpage"]);
    if (response.statusCode == 200) {
      print(response);
      return json.decode(response.toString())["data"];
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getBookCatalog(String id) async {
  try {
    print("开始获取书的章节页面");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["bookcatalogpage"] + id);
    if (response.statusCode == 200) {
      print(response);
      return {
        "bookcatalog": json.decode(response.toString())["chapters"],
        "book": json.decode(response.toString())["book"],
        "section": json.decode(response.toString())["section"]
      };
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getBookDocs(String id) async {
  try {
    print("开始获取节的内容页面");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["bookdocspage"] + id);
    if (response.statusCode == 200) {
      print(response);
      return {
        "data": json.decode(response.toString())["data"],
        "iflast": json.decode(response.toString())["iflast"],
        "ifnext": json.decode(response.toString())["ifnext"],
      };
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getnextDocs(String id) async {
  try {
    print("开始获取书的下一页");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["booknextdocs"] + id);
    if (response.statusCode == 200) {
      print(response);
      return {
        "data": json.decode(response.toString())["data"],
        "iflast": json.decode(response.toString())["iflast"],
        "ifnext": json.decode(response.toString())["ifnext"],
      };
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}

Future getlastDocs(String id) async {
  try {
    print("开始获取书的上一页");
    Response response;
    Dio dio = new Dio();
    response = await dio.get(servicePath["booknextdocs"] + id);
    if (response.statusCode == 200) {
      print(response);
      return {
        "data": json.decode(response.toString())["data"],
        "iflast": json.decode(response.toString())["iflast"],
        "ifnext": json.decode(response.toString())["ifnext"],
      };
    } else {
      throw Exception("后台接口出现异常");
    }
  } catch (e) {
    return print('ERROR:============>${e}');
  }
}
